package com.jsgt.greekfire.dao.model

import android.os.Parcel
import android.os.Parcelable
import androidx.annotation.NonNull
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import androidx.room.TypeConverters
import com.jsgt.greekfire.common.util.AESOperator
import com.jsgt.greekfire.common.util.MD5Util
import com.jsgt.greekfire.dao.DateTypeConverter


/**
 * author fhx
 * create time 2020/4/23 17:06
 **/
@Entity(tableName = "identity")
class IdentityBean() : Parcelable {

    @PrimaryKey(autoGenerate = true)
    var id: Int = 0 //主键递增id

    @ColumnInfo(name = "name")
    var identityName: String? = null  //名称

    @ColumnInfo
    var mnemonic: String? = null  //助记词

    @ColumnInfo
    var priKey: String? = null //私钥

    @ColumnInfo
    var password: String? = null //密码

    @ColumnInfo(name = "password_tips")
    var passwordTips: String? = null //密码提示

    @ColumnInfo
    @TypeConverters(DateTypeConverter::class)
    var backup: Boolean = false //是否已备份

    @ColumnInfo
    var portrait: String? = null //头像

    fun setMnemonicByAES(m: String) {
        if(m.isEmpty()){
            return
        }
        mnemonic = AESOperator.getInstance().encryptJni(m)
    }

    fun getMnemonicByAES(): String {
        if (mnemonic.isNullOrEmpty()){
            return ""
        }
        return AESOperator.getInstance().decryptJni(mnemonic)
    }

    fun setPriKeyByAES(m: String) {
        if(m.isEmpty()){
            return
        }
        priKey = AESOperator.getInstance().encryptJni(m)
    }

    fun getPriKeyByAES(): String {
        if (priKey.isNullOrEmpty()){
            return ""
        }
        return AESOperator.getInstance().decryptJni(priKey)
    }

    fun setPasswordByMd5(m: String) {
        if(m.isEmpty()){
            return
        }
        password = MD5Util.getMD5(m)
    }

    constructor(parcel: Parcel) : this() {
        id = parcel.readInt()
        identityName = parcel.readString()
        mnemonic = parcel.readString()
        priKey = parcel.readString()
        password = parcel.readString()
        passwordTips = parcel.readString()
        backup = parcel.readByte() != 0.toByte()
        portrait = parcel.readString()
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeInt(id)
        parcel.writeString(identityName)
        parcel.writeString(mnemonic)
        parcel.writeString(priKey)
        parcel.writeString(password)
        parcel.writeString(passwordTips)
        parcel.writeByte(if (backup) 1 else 0)
        parcel.writeString(portrait)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<IdentityBean> {
        override fun createFromParcel(parcel: Parcel): IdentityBean {
            return IdentityBean(parcel)
        }

        override fun newArray(size: Int): Array<IdentityBean?> {
            return arrayOfNulls(size)
        }
    }

    data class Backup(val id: Int, val backup: Boolean)

    data class Name(val id: Int, val name: String)
}